<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Framework_Cache
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */


require_once "Cache/Lite.php";

/**
 * Framework_Cache
 *
 * This class is a factory loader for PEAR's Cache_Lite package
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @see http://pear.php.net/package/Cache_Lite
 * @package Framework
 *
 */
abstract class Framework_Cache
{

	/**
     * $instances
     *
     * @access 		public
     * @static
     * @var 		array 		$instances
     */
	static private $instances = array();

	/**
	 * factory
	 *
	 * @access public
	 * @param string $cache Name of the cache to load
	 * @return mixed Instance of Cache_Lite, PEAR_Error
	 * @static
	 */
	static public function factory($cache = 'Framework', $lifeTime = 14400)
	{

		if (!array_key_exists($cache,self::$instances)) {

			$rootPath = Framework::$site->getPath().'/Cache';
				
			if (!is_dir($rootPath) || !is_writable($rootPath)) {
				return PEAR::raiseError('Invalid root cache directory');
			}
				
			$cacheDir =  $rootPath.'/'.$cache.'/';
				
			if (!is_dir($cacheDir)) {

				if (!mkdir($cacheDir)) {
					return PEAR::raiseError('Could not create cache directory');
				}

			}
			
			$options = array(
	    		'cacheDir' => $cacheDir,
	    		'lifeTime' => $lifeTime,
				'automaticSerialization' => true,
				'automaticCleaningFactor' => 20
			);
				
			$instance = new Cache_Lite($options);
			if (PEAR::isError($instance)) {
				return PEAR::raiseError('Could not load cache: '.$instance->getMessage());
			} else {
				self::$instances[$cache] = $instance;

				return $instance;
			}
		} else {
			return self::$instances[$cache];
		}


	}
	
	
	/**
	 * handlerFactory
	 *
	 * @access 		public
	 * @param 		string 		$handler
	 * @param 		object		$object
	 * @return 		mixed 		Instance of Framework_Cache_Handler_Common, PEAR_Error
	 * @static
	 */
	static public function handlerFactory($handler, $object)
	{
		$file = 'Framework/Cache/Handler/'.$handler.'.php';
        if (!include_once($file)) {
            return PEAR::raiseError('Could not load site file: '.$file);
        }

        $class = 'Framework_Cache_Handler_'.$handler;
        if (!class_exists($class)) {
            return PEAR::raiseError('Could not find handler class: '.$class); 
        }

        try {
            $instance = new $class($object);
            if (!$instance instanceof Framework_Cache_Handler_Common) {
            	return PEAR::raiseError('Cache handler class must extend from Framework_Cache_Handler_Common');
            }
        } catch (Framework_Exception $error) {
            return PEAR::raiseError($error->getMessage());
        }

        return $instance;
		
	}

}

?>